#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
性能测试脚本
"""

import time
import random
from database import Database
from ui.transaction_widget import TransactionWidget
from ui.main_window import MainWindow
from PyQt5.QtWidgets import QApplication
import sys

def generate_test_data(num_records=1000):
    """生成测试数据"""
    categories = [
        "餐饮|早餐", "餐饮|午餐", "餐饮|晚餐", "餐饮|零食",
        "交通|公交", "交通|地铁", "交通|打车", "交通|加油",
        "购物|日用品", "购物|服装", "购物|电子产品", "购物|书籍",
        "娱乐|电影", "娱乐|游戏", "娱乐|旅游", "娱乐|聚会",
        "医疗|药品", "医疗|检查", "医疗|治疗",
        "教育|学费", "教育|培训", "教育|书籍",
        "生活|房租", "生活|水电", "生活|通讯", "生活|物业",
        "收入|工资", "收入|奖金", "收入|投资", "收入|其他"
    ]
    
    data = []
    for i in range(num_records):
        # 随机生成交易记录
        timestamp = f"2024-{random.randint(1, 12):02d}-{random.randint(1, 28):02d} {random.randint(0, 23):02d}:{random.randint(0, 59):02d}:{random.randint(0, 59):02d}"
        amount = round(random.uniform(1, 1000), 2)
        transaction_type = random.choice(["收入", "支出"])
        category = random.choice(categories)
        payee = f"商户{random.randint(1, 100)}"
        source = random.choice(["微信", "支付宝", "银行卡"])
        note = f"测试备注{i}"
        
        data.append((timestamp, amount, transaction_type, category, payee, source, note))
    
    return data

def test_database_performance():
    """测试数据库性能"""
    print("开始数据库性能测试...")
    
    # 生成测试数据
    test_data = generate_test_data(1000)
    
    # 初始化数据库
    db = Database()
    
    # 测试批量插入性能
    start_time = time.time()
    db.insert_transactions(test_data)
    insert_time = time.time() - start_time
    print(f"批量插入1000条记录耗时: {insert_time:.4f}秒")
    
    # 测试查询性能
    start_time = time.time()
    transactions = db.get_transactions("2024-01-01 00:00:00", "2024-12-31 23:59:59")
    query_time = time.time() - start_time
    print(f"查询{len(transactions)}条记录耗时: {query_time:.4f}秒")
    
    # 测试分类查询性能
    start_time = time.time()
    categories = db.get_categories()
    category_query_time = time.time() - start_time
    print(f"查询分类耗时: {category_query_time:.4f}秒")
    
    return insert_time, query_time, category_query_time

def test_ui_performance():
    """测试UI性能"""
    print("\n开始UI性能测试...")
    
    # 初始化QApplication
    app = QApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    
    # 测试主窗口初始化性能
    start_time = time.time()
    main_window = MainWindow()
    init_time = time.time() - start_time
    print(f"主窗口初始化耗时: {init_time:.4f}秒")
    
    # 测试交易明细面板数据加载性能
    start_time = time.time()
    # 直接调用加载方法而不是通过标签页切换
    main_window.transaction_widget.load_data()
    load_time = time.time() - start_time
    print(f"交易明细面板数据加载耗时: {load_time:.4f}秒")
    
    return init_time, load_time

def main():
    """主函数"""
    print("=== 记账助手性能测试 ===")
    
    # 测试数据库性能
    db_insert_time, db_query_time, db_category_time = test_database_performance()
    
    # 测试UI性能
    ui_init_time, ui_load_time = test_ui_performance()
    
    # 输出总结
    print("\n=== 性能测试总结 ===")
    print(f"数据库批量插入: {db_insert_time:.4f}秒")
    print(f"数据库查询: {db_query_time:.4f}秒")
    print(f"分类查询: {db_category_time:.4f}秒")
    print(f"UI初始化: {ui_init_time:.4f}秒")
    print(f"数据加载: {ui_load_time:.4f}秒")
    
    total_time = db_insert_time + db_query_time + db_category_time + ui_init_time + ui_load_time
    print(f"\n总耗时: {total_time:.4f}秒")

if __name__ == "__main__":
    main()